package com.ubis.ibms.plugin.dialogs;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.ubis.ibms.plugin.composites.company.CompanyQueryComposite;
import com.ubis.ibms.plugin.dao.RechargeManager;
import com.ubis.ibms.plugin.model.Company;

public class RechargeAddDialog extends Dialog {
	private DataBindingContext m_bindingContext;
	
	// 公司名称
	private Text nameText;
	// 联系人
	private Text perText;
	// 电话
	private Text telText;
	//充值金额
	private Text rechargeText;
	//确认充值
	private Button okButton;
	
	private Composite container;
	
	private Company company;
		
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public RechargeAddDialog(Shell parentShell, Company company) {
		super(parentShell);
		this.company = company;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(3, false));
		
		// 公司名称
		Label nameLabel = new Label(container, SWT.RIGHT);
		nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		nameLabel.setText("公司名称：");
		nameText = new Text(container, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				2, 1));
		nameText.setEditable(false);
		
		// 联系人
		Label perLabel = new Label(container, SWT.RIGHT);
		perLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		perLabel.setText("联系人：");
		perText = new Text(container, SWT.BORDER);
		perText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				2, 1));
		perText.setEditable(false);

		// 电话
		Label telLabel = new Label(container, SWT.RIGHT);
		telLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		telLabel.setText("电话：");
		telText = new Text(container, SWT.BORDER);
		telText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				2, 1));
		telText.setEditable(false);
		
		// 充值金额
		Label rechargeLabel1 = new Label(container, SWT.RIGHT);
		rechargeLabel1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		rechargeLabel1.setText("充值金额：");
		rechargeText = new Text(container, SWT.BORDER);
		rechargeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		Label rechargeLabel2 = new Label(container, SWT.LEFT);
		rechargeLabel2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		rechargeLabel2.setText("元");
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				false, 1, 1));
		parent.setLayout(new GridLayout(2, false));
		okButton = createButton(parent, IDialogConstants.CLIENT_ID, "充值",
				false);
		okButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		okButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				try{
					double value = Double.parseDouble(rechargeText.getText());
					if(value>0){//充值金额要大于0
						RechargeManager.saveRecharge(company.getId(), company.getName(), value);
						MessageDialog.openInformation(RechargeAddDialog.this.getShell(), "充值成功",
								"充值成功！");
						close();
					}
					else
						MessageDialog.openWarning(RechargeAddDialog.this.getShell(), "警告", "请输入正确充值金额！");
				}
				catch(Exception ex){
					MessageDialog.openWarning(RechargeAddDialog.this.getShell(), "警告", "请输入正确充值金额！");
				}
			}
		});
		m_bindingContext = initDataBindings();
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(300, 200);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("充值");
	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue observeTextNameTextObserveWidget = WidgetProperties.text(SWT.Modify).observe(nameText);
		IObservableValue nameCompanyObserveValue = PojoProperties.value("name").observe(company);
		bindingContext.bindValue(observeTextNameTextObserveWidget, nameCompanyObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
		//
		IObservableValue observeTextPerTextObserveWidget = WidgetProperties.text(SWT.Modify).observe(perText);
		IObservableValue perCompanyObserveValue = PojoProperties.value("per").observe(company);
		bindingContext.bindValue(observeTextPerTextObserveWidget, perCompanyObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
		//
		IObservableValue observeTextTelTextObserveWidget = WidgetProperties.text(SWT.Modify).observe(telText);
		IObservableValue telCompanyObserveValue = PojoProperties.value("tel").observe(company);
		bindingContext.bindValue(observeTextTelTextObserveWidget, telCompanyObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
		//
		return bindingContext;
	}
}
